package myself.register.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import myself.entity.TeacherInfoEntity;
import myself.register.service.RegisterService;
import myself.util.MD5;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * 新增页面
 * 用于 教师用户的注册
 * 
 * */
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RegistController {

	Logger logger = LoggerFactory.getLogger(RegistController.class);
	
	
	@Resource
	private RegisterService registerService;

	@RequestMapping(value = "/register.do")
	public ModelAndView main(HttpServletRequest request) {
		logger.info("跳转到注册页面!");

		return new ModelAndView("WEB-INF/jsp/register/register.jsp");
	}

	// 主要是想用一下 MD5加密,要返回一个工号给前端,坐登陆用，用到同步验证方法,前台传密码数据过来,MD5加密后存入数据库
	@RequestMapping(value = "validationOf", method = RequestMethod.GET)
	public @ResponseBody String validationOf(HttpServletRequest request,
			@RequestParam("userName") String userName,
			@RequestParam("password") String password) {
		String resultCode = "9999";
     
		//密码要进行MD5加密
		String passwordByMD5 = new MD5().getMD5ofStr(password);
		
		//查询出当前最大的teacher_code
		String teacherCode = Integer.parseInt(registerService.qryMaxTeacherCode())+1+"";
		
        TeacherInfoEntity teacherInfoEntity = new TeacherInfoEntity();
        
        teacherInfoEntity.setTeacherCode(teacherCode);
        teacherInfoEntity.setTeacherName(userName);
        teacherInfoEntity.setTeacherPassword(passwordByMD5);
        try {
			registerService.insertToTeacher(teacherInfoEntity);
			resultCode = teacherCode;
		} catch (Exception e) {
			logger.error("插入数据库失败！",e);
			e.printStackTrace();
		}
        
		return resultCode;
	}

}
